package com.utf999.qiangmanger.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.orm.OrderTypeDefineEntity;
import com.utf999.qiangmanger.entity.orm.OrderTypeEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.IOrderTypeDefineService;
import com.utf999.qiangmanger.service.iface.IOrderTypeService;

/**
 * @author yinwenjie
 *
 */
@Controller
@RequestMapping(value={"/ordertype"})
public class OrderTypeController extends BaseController {
	@Autowired
	private IOrderTypeService orderTypeService;
	
	@Autowired
	private IOrderTypeDefineService orderTypeDefineService;
	
	/**
	 * 日志信息
	 */
	private static Log controllerLog4j = LogFactory.getLog(OrderTypeController.class);
	
	/**
	 * 在超级管理员进行“订单类型”管理之前，会首先通过这个方法进行数据初始化。
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value={"/preOrderTypeList"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView preOrderTypeList(HttpServletRequest request , HttpServletResponse response) throws BizException {
		ModelAndView mv = new ModelAndView("workorder/orderTypelist");
		return mv;
	}
	
	/**
	 * 根据公司编号查询订单类型信息（无论这些订单类型的状态如何）<br>
	 * 包括这个订单对应的用户组信息、奖惩信息
	 * @param companyid 指定的公司编号信息
	 * @return
	 * @throws BizException
	 */
	@RequestMapping(value={"/queryOrderTypesByCompanyid"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryOrderTypesByCompanyid(HttpServletRequest request , HttpServletResponse response , String companyid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<OrderTypeEntity> types = null;
		try {
			types = this.orderTypeService.queryOrderTypesByCompanyid(companyid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(types);
			this.writeResponseMsg(response, jsonEntity , new String[]{"orderType" , "company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 根据工单类型编号查询订单类型信息<br>
	 * @param ordertypeid 指定的工单类型编号
	 * @throws BizException
	 */
	@RequestMapping(value={"/queryOrderTypesByid"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryOrderTypesByid(HttpServletRequest request , HttpServletResponse response , String ordertypeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		OrderTypeEntity type = null;
		try {
			type = (OrderTypeEntity)this.orderTypeService.getEntity(ordertypeid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(type);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "defines" , "userGroups"});
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 根据工单类型编号查询指定的公司下的工单类型信息
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param orderCode 指定的工单类型编号
	 */
	@RequestMapping(value={"/queryOrderTypeByCode"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryOrderTypeByCode(HttpServletRequest request , HttpServletResponse response , String companyid , String orderCode) {
		JsonEntity jsonEntity = new JsonEntity();
		
		OrderTypeEntity type = null;
		try {
			type = this.orderTypeService.queryOrderTypeByCode(companyid, orderCode);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(type);
			this.writeResponseMsg(response, jsonEntity , new String[]{"orderType", "company" , "orderType" , "uniqueKey"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 查询指定的工单类型下对应的奖惩信息。并且按照durationChange进行排序（这样是为了更好看出来递增递减顺序）
	 * @param request
	 * @param response
	 * @param ordertypeid 
	 */
	@RequestMapping(value={"/queryOrderTypeDefine"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void queryOrderTypeDefine(HttpServletRequest request , HttpServletResponse response , String ordertypeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<OrderTypeDefineEntity> defines = null;
		try {
			defines = this.orderTypeDefineService.queryOrderTypeDefine(ordertypeid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(defines);
			this.writeResponseMsg(response, jsonEntity , new String[]{"orderType"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 为一个指定的公司创建一个自定义的工单类型信息
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param orderTypeName 新创建的订单类型名称
	 * @param orderCode 新创建的工单类型编号（每一个公司下，只有为一个的一个工单类型编号）
	 * @param integral 工单类型默认的积分数量
	 * @param duration 工单类型默认的标准处理时长
	 * @throws BizException
	 */
	@RequestMapping(value={"/createOrderType"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void createOrderType(HttpServletRequest request , HttpServletResponse response ,String companyid, String orderTypeName, String orderCode , Integer integral, Integer duration) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeService.createOrderType(companyid, orderTypeName, orderCode, integral, duration);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 根据工单类型编号查询指定的公司下的工单类型信息
	 * (查询指定的工单类型是否存在)
	 * @param request
	 * @param response
	 * @param companyid 指定的公司编号
	 * @param orderCode 指定的工单类型编号
	 */
	@RequestMapping(value={"/existOrderTypeByCode"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void existOrderTypeByCode(HttpServletRequest request , HttpServletResponse response , String companyid , String orderCode) {
		JsonEntity jsonEntity = new JsonEntity();
		
		OrderTypeEntity type = null;
		try {
			type = this.orderTypeService.existOrderTypeByCode(companyid, orderCode);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			if(type != null) {
				jsonEntity.setData(true);
			} else {
				jsonEntity.setData(false);
			}
			this.writeResponseMsg(response, jsonEntity , new String[]{"orderType"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 向指定的工单类型中增加
	 * @param request 
	 * @param response 
	 * @param ordertypeid 对应的工单类型编号
	 * @param durationChange 时间变化情况：单位为秒，注意只记录差量。
	 * @param integralChange 积分变化情况：单位为%(百分数)，注意只记录差量。
	 * @throws BizException
	 */
	@RequestMapping(value={"/appendOrderTypeDefine"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void appendOrderTypeDefine(HttpServletRequest request , HttpServletResponse response , String ordertypeid , Integer durationChange , Integer integralChange) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeDefineService.appendOrderTypeDefine(ordertypeid, durationChange, integralChange);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 修改一个指定的工单类型
	 * @param request
	 * @param response
	 * @param orderTypeid 指定的工单类型编号
	 * @param orderTypeName 修改的工单类型的名称
	 * @param orderCode 修改的工单类型编号
	 * @param integral 工单类型默认的积分数量
	 * @param duration 工单类型默认的标准处理时长
	 * @throws BizException
	 */
	@RequestMapping(value={"/modifyOrderType"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void modifyOrderType(HttpServletRequest request , HttpServletResponse response , String orderTypeid, String orderTypeName, String orderCode , Integer integral, Integer duration) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeService.modifyOrderType(orderTypeid, orderTypeName, orderCode , integral, duration);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 更改指定订单类型的状态。在这种情况下，一般是超级管理员禁用/启用订单类型的使用
	 * @param request
	 * @param response
	 * @param ordertypeid 指定的订单类型id
	 */
	@RequestMapping(value={"/updateOrderTypeStatus"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void updateOrderTypeStatus(HttpServletRequest request , HttpServletResponse response , String ordertypeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			OrderTypeEntity type = (OrderTypeEntity)this.orderTypeService.getEntity(ordertypeid);
			if(type.getStatus() == 1) {
				type.setStatus(0);
			} else {
				type.setStatus(1);
			}
			this.orderTypeService.update(type);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 将一个指定的工单类型 绑定到一个指定的用户组中。<br>
	 * 注意，如果这个工单类型已经在这个用户组中了，系统将会报错。
	 * @param request
	 * @param response
	 * @param ordertypeid 指定的工单类型编号
	 * @param groupids 指定的用户分组编号(可以只有一个，可以有多个，但是不能没有)
	 * @throws BizException
	 */
	@RequestMapping(value={"/bindUsergroupMapping"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin } , promptMode=PromptModeType.AJAX)
	public void bindUsergroupMapping(HttpServletRequest request , HttpServletResponse response , String ordertypeid , String... groupids) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeService.bindUsergroupMapping(ordertypeid, groupids);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 将一个指定的工单类型 绑定到一个指定的用户组中。<br>
	 * 注意，如果这个工单类型已经在这个用户组中了，系统将会报错。
	 * @param request
	 * @param response
	 * @param ordertypeid 指定的工单类型编号
	 * @param groupids 指定的用户分组编号(可以只有一个，可以有多个，但是不能没有)
	 * @throws BizException
	 */
	@RequestMapping(value={"/unBindUsergroupMapping"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin  , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void unBindUsergroupMapping(HttpServletRequest request , HttpServletResponse response , String ordertypeid , String... groupids) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeService.unbindUsergroupMapping(ordertypeid, groupids);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 删除指定的奖惩定义
	 * @param request
	 * @param response
	 * @param defineid 将要被删除的奖惩定义编号
	 * @throws BizException
	 */
	@RequestMapping(value={"/removeOrderTypeDefine"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin  , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void removeOrderTypeDefine(HttpServletRequest request , HttpServletResponse response , String defineid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.orderTypeDefineService.removeOrderTypeDefine(defineid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
}
